import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "../store/base";
import mall from "../store/mall";
import router from '../router'
import config from '@src/config';
import { tabDumpFunc } from '@src/common/dump'
const initData = (tabBar) => {
  let temp = tabBar.map(value => {
    if (value.entrancePath === "/pages/mineScan/main") {
      return {
        "entranceCode": "MEMBER_CODE",
        "entrancePath": "pages/mineScan/main",
        "name": value.name,
        "icon": "/static/tabbar/tabbar-scan-n.png",
        "selectedIcon": "/static/tabbar/tabbar-scan-h.png"
      };
    }
    return value
  })
  return [
    {
      "entranceCode": "indexPage",
      "entrancePath": "pages/index/main",
      "name": "会员卡",
      "icon": "/static/tabbar/tabbar-index-n.png",
      "selectedIcon": "/static/tabbar/tabbar-index-h.png"
    },
    ...temp,
    {
      "entranceCode": "minePage",
      "entrancePath": "pages/mine/main",
      "name": "我的",
      "icon": "/static/tabbar/tabbar-mine-n.png",
      "selectedIcon": "/static/tabbar/tabbar-mine-h.png"
    }]
}

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['isInit', 'tabBar', 'isiPhoneX', 'themeColor']
  },
  {
    store: mall,
    fields: ['storeId']
  }],
  data: {
    show: true,
    login: true,
    tpl: config.tpl,
    selected: '',
    tabBarList: [],
  },
  observers: {
    'tabBar': function (tabBar) {
      this.initTabBar(tabBar);
    }
  },
  methods: {
    hide() {
      this.setData({ show: false })
    },
    show() {
      this.setData({ show: true })
    },
    select(selected) {
      this.setData({ selected: selected });
      console.log('selected', this.data.selected)
    },
    initTabBar(data) {
      if (config.tpl === 'default') {
        this.setData({ tabBarList: initData(data) })
      } else {
        this.setData({ tabBarList: data })
      }
    },
    switchTab: function (e) {
      const item = e.currentTarget.dataset.item;
      console.info("item", item)
      if (router.tabBars.indexOf(item.openPath) === -1) {
        console.log("-1")
        let pages = getCurrentPages();
        pages[pages.length - 1].selectComponent("#login").requireLogin().then(res => {
          tabDumpFunc(item);
          //router.push({ path: item.openPath })
          // if (item.openInsidePage == 'goodsDetail') {
          //   let insideParams = JSON.parse(item.openInsideParams);
          //   let urlParams = {
          //     storeId: this.data.storeId,
          //     goodsSerial: insideParams.goodsSerial
          //   }
          //   router.push({ name: item.openPath, params: urlParams });
          // } else {
          //   console.log('item', item)
          //   console.log('item.openPath', item.openPath)
          //   router.push({ name: item.openPath });
          // }
        })
        return;
      }
      tabDumpFunc(item, true);
      // router.push({ path: item.openPath })
    },
    switchTabOld: function (e) {
      const data = e.currentTarget.dataset;
      if (router.tabBars.indexOf(data.path) === -1) {
        console.info("")
        let pages = getCurrentPages();
        pages[pages.length - 1].selectComponent("#login").requireLogin().then(res => {
          router.push({ path: data.path })
        });
        return;
      }
      router.push({ path: data.path })
    }
  }
})
